<?php
namespace plugin\ky_black\app\admin\service;

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

class JwtService
{
    /**
     * JWT密钥
     */
    protected $key = 'your-secret-key';

    /**
     * token过期时间（秒）
     */
    protected $expire = 86400;

    /**
     * 创建token
     */
    public function createToken(array $payload): string
    {
        $payload['iat'] = time();
        $payload['exp'] = time() + $this->expire;

        return JWT::encode($payload, $this->key, 'HS256');
    }

    /**
     * 验证token
     */
    public function verifyToken(string $token)
    {
        try {
            return JWT::decode($token, new Key($this->key, 'HS256'));
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * 从token中获取用户ID
     */
    public function getStaffId(string $token)
    {
        $payload = $this->verifyToken($token);
        return $payload ? $payload->staff_id : null;
    }
} 